// 1. 引入
import Vue from 'vue'
import Router from 'vue-router'

import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'
import userRouter from './modules/user'

Vue.use(Router)

import Layout from '@/layout'

/**
 * 静态路由对象数组
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '主页', icon: 'dashboard' }
    }
    ]
  },
  {
    path: '/import',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/import')
      }
    ],
    hidden: true
  },
  userRouter
  // { path: '*', redirect: '/404', hidden: true }
]

/**
 * 动态路由
 */
export const asyncRoutes = [
  departmentsRouter,
  settingRouter,
  employeesRouter,
  approvalsRouter,
  permissionRouter,
  attendancesRouter,
  salarysRouter,
  socialRouter
]

/**
 * 创建一个路由实例对象函数
 */
const createRouter = () => new Router({
  mode: 'history', // 路由模式: hash history
  scrollBehavior: () => ({ y: 0 }), // 路由切换时让页面滚动到顶部
  // routes: [...constantRoutes, ...asyncRoutes]
  routes: [...constantRoutes]
})

/**
 * 创建一个路由对象
 */
const router = createRouter()

/**
 * 重置路由规则数组(后期做权限控制的时候会用到)
 */
export function resetRouter() {
  const newRouter = createRouter()
  // console.log(router.matcher, '-----------')
  router.matcher = newRouter.matcher // reset router
  // console.log(router.matcher, '+++++++++++')
}

/**
 * 导出路由对象
 */
export default router

/*
    常用的写法: 路由懒加载(用到的时候再去加载)
    component: () => import('@/views/login/index'),

    无论是 一次性导入  还是按需加载
    只要导入一次, 第二次都是从缓存中读取
*/
